package com.glorysoft.lipuxinpda.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.glorysoft.lipuxinpda.R;
import com.glorysoft.lipuxinpda.base.BaseActivity;
import com.glorysoft.lipuxinpda.base.Constant;
import com.glorysoft.lipuxinpda.entity.BaseEntity;
import com.glorysoft.lipuxinpda.entity.EqpLockInfoEntity;
import com.glorysoft.lipuxinpda.entity.SpotCheckInfoReturnEntity;
import com.glorysoft.lipuxinpda.entity.SpotCheckListInfoReturnEntity;
import com.glorysoft.lipuxinpda.utils.GsonUtils;
import com.glorysoft.lipuxinpda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

public class UnlockActivity extends BaseActivity {
    private Context mContext;

    private LinearLayout llMain;

    private TextView tv_current_eqp_no;

    private String sEqpID;//设备号
    private String sCheckType;//解锁点检类型
    private String[] arrCheckInfo;//解锁点检类型

    private FreeUI_GeneralFragmentDialog mMessageDialog;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_unlock;
    }

    @Override
    protected void initView() {
        mContext = this;

        llMain = findViewById(R.id.llMain);
        tv_current_eqp_no = findViewById(R.id.tv_current_eqp_no);

    }

    @Override
    protected void initData() {
        Bundle extras = getIntent().getExtras();
        sEqpID = extras.getString("EQPID");

        tv_current_eqp_no.setText(sEqpID);

        eqpLockInfo();//获取设备锁机类型

        //region 测试
//        String sJson = "{" +
//                "     \"data\":{\n" +
//                "        \"EQPID\":\"2FS106\",\n" +
//                "        \"LOCKCAUSE\" : \"报警锁机;点检锁机;其他锁机\"\n" +
//                "}    \n" +
//                "}";
//
//        try {
//            JSONObject jsonObject = new JSONObject(sJson);
//            String sData = jsonObject.getString("data");
//            arrCheckInfo = GsonUtils.getObject(sData, EqpLockInfoEntity.class).getLOCKCAUSE().split(";");
//            setCheckInfo();//设置点检类型
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }


        //endregion
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle(getString(R.string.unlock));
        freeUI_titleView.setRightListener(getString(R.string.Submit), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showMessageDialog(getResources().getString(R.string.is_confirm_submit));
            }
        });
        return new FreeUI_AddViewEntity(getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY), llMain);
    }

    //region 接口调用

    //获取设备锁机类型
    private void eqpLockInfo() {
        FreeUI_EntityCallBack<BaseEntity<EqpLockInfoEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseEntity<EqpLockInfoEntity>>(new TypeToken<BaseEntity<EqpLockInfoEntity>>() {
        }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseEntity<EqpLockInfoEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getSupportFragmentManager())) {
                    showToast(response.body().getMessage(), true);
                    String checkInfo = response.body().getData().getBody().getLOCKCAUSE();
                    arrCheckInfo = checkInfo.split(";");
                    setCheckInfo();//设置点检类型
                }
            }

            @Override
            public void onError(Response<BaseEntity<EqpLockInfoEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "EqpLockInfo");
            }
        };

        HashMap<String, String> map = new HashMap<String, String>();

        map.put("EQP_ID", sEqpID);//设备号
        map.put("CUR_USERID", Constant.INSTANCE.getLoginEntity().getWORK_NO());//工号
        String method = Constant.INSTANCE.getNET_URL() + "/EqpLockInfo";
        new NetUtils().requestPostNetNews(method, this, map, callBack);
    }

    //解锁
    private void changeEqpLockStatus(){
        FreeUI_EntityCallBack<BaseEntity> callBack = new FreeUI_DialogEntityCallBack<BaseEntity>(new TypeToken<BaseEntity>() {
        }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseEntity> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getSupportFragmentManager())) {
                    showDialog("[" + sCheckType + "]" + response.body().getMessage(), true);
//                    showToast(response.body().getMessage(), true);
                }
            }

            @Override
            public void onError(Response<BaseEntity> response) {
                super.onError(response);
                loadError(response.getException(), "ChangeEqpLockStatus");
            }
        };

        HashMap<String, String> map = new HashMap<String, String>();

        map.put("EQP_ID", sEqpID);//设备号
        map.put("CUR_USERID", Constant.INSTANCE.getLoginEntity().getWORK_NO());//工号
        map.put("LOCKCAUSE", sCheckType);//解锁锁机类型

        String method = Constant.INSTANCE.getNET_URL() + "/ChangeSpotCheckInfo";
        new NetUtils().requestPostNetNews(method, this, map, callBack);
    }

    //endregion

    //显示点检项
    private void setCheckInfo() {
        if (arrCheckInfo.length <= 0) {
            return;
        }
        Spinner m_Spinenr = findViewById(R.id.spinner);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.item_spinner_textview, arrCheckInfo);
        //参数2是样式   R.layout.custom_spiner_text_item 是自定义的样式 是在res/layout/ 目录下 一个名为  custom_spiner_text_item  文件里定义的样式  如下

        //系统自带样式有三种
        // simple_spinner_item   simple_spinner_dropdown_item   simple_expandable_list_item_1

        m_Spinenr.setAdapter(adapter);
        m_Spinenr.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                sCheckType = arrCheckInfo[pos];  //取出选中的值
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });


    }

    private void showMessageDialog(String message) {
        mMessageDialog = new FreeUI_GeneralFragmentDialog();
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_prompt2, null);
        Button btnConfirm = view.findViewById(R.id.btnConfirm);
        Button btnCancel = view.findViewById(R.id.btnCancel);
        TextView tvTips = view.findViewById(R.id.tvTips);
        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMessageDialog.dismiss();
                changeEqpLockStatus();//解锁

                //region 测试
//                showDialog("[" + sCheckType + "]" + "解锁成功,设备已恢复请手动开始", true);
                //endregion

            }
        });
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMessageDialog.dismiss();
            }
        });
        tvTips.setText(message);
        mMessageDialog.setLayout(view);
//        mDieTypeDialog.getDialog().setCanceledOnTouchOutside(false);
        mMessageDialog.show(getSupportFragmentManager(), "isDelete");
        mMessageDialog.setCallBack(new FreeUI_CommonCallback() {
            @Override
            public void onSuccess() {
                mMessageDialog = null;
            }
        });

    }

}